package roborally.program.command;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import roborally.itemsonboard.Robot;
import be.kuleuven.cs.som.annotate.Basic;
import be.kuleuven.cs.som.annotate.Immutable;

/**
 * A class representing a conditional statement.
 * It consists of a condition and two commands. 
 * The first is executed if the condition is true, the other if the condition is false.
 * 
 * @author Molderez Tom, 2de bach Ingenieurswetenschappen Computerwetenschappen-Elektrotechniek and Reniers Jorn, 2de bach Ingenieurswetenschappen Werktuigkunde-Computerwetenschappen
 * @version 1.0
 *
 */
public class IfCommand extends ConditionalCommand {

	private static final String IF  = "if";
	
	/**
	 * Creates a new IfCommand.
	 * 
	 */
	public IfCommand(Condition condition) {
		super(condition);
	}
	
	public void addAsFalseCommands(Collection<? extends Command> commands) {
		falseCommands.addAll(commands);
	}
	
	private final List<Command> falseCommands = new ArrayList<Command>();

	/**
	 * Execute this IfCommand.
	 */
	@Override
	public void execute(Robot robot) {
		this.getCondition().execute(robot);
		if(this.getCondition().getReturnValue())
			super.execute(robot); //uitvoeren van de commando's in een flowcommand.
		else
			for(Command lusCommand : falseCommands)
				lusCommand.execute(robot);
	}

	/**
	 * Return a String representation of this IfCommand.
	 */
	@Override
	public String write() {
		String write = new String();
		
		write = write + " (" + this.getCondition().write() + ")";
		
		write = write + " (" + super.write() + ")";
		
		write = write + " (";
		for(Command command : this.getFalseCommands())
			write = write + command.write();
		write = write + ")";
		
		return "(" + IF + write + ")";
	}

	@Basic
	public List<Command> getFalseCommands() {
		return Collections.unmodifiableList(falseCommands);
	}

}
